package com.gxcc3.ygps.system.service.impl;

import com.gxcc3.ygps.common.constant.Constants;
import com.gxcc3.ygps.common.constant.UserConstants;
import com.gxcc3.ygps.common.core.domain.entity.SysCompany;
import com.gxcc3.ygps.common.core.domain.entity.SysCompanyMenu;
import com.gxcc3.ygps.common.core.domain.entity.SysIotCar;
import com.gxcc3.ygps.common.core.domain.entity.SysIotStorage;
import com.gxcc3.ygps.common.exception.ServiceException;
import com.gxcc3.ygps.common.utils.StringUtils;
import com.gxcc3.ygps.system.mapper.SysIotCarMapper;
import com.gxcc3.ygps.system.service.ISysIotCarService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysIotCarServiceImpl implements ISysIotCarService {

    @Resource
    SysIotCarMapper carMapper;


    /**
     * 获取车辆列表
     * @param car 查询参数
     * @return 查询结果
     */
    @Override
    public List<SysIotStorage> selectCarList(SysIotCar car) {
        return carMapper.selectCarList(car);
    }


    /**
     * 添加车辆
     * @param car 参数
     * @return 结果
     */
    @Override
    public int insertCar(SysIotCar car) {
        return carMapper.insertCar(car);
    }


    /**
     * 查询车辆
     * @param id 主键
     * @return 结果
     */
    @Override
    public SysIotCar selectCarById(Long id) {
        return carMapper.selectCarById(id);
    }


    /**
     * 修改车辆
     * @param car 参数
     * @return 结果
     */
    @Override
    public int updateCar(SysIotCar car) {
        return carMapper.updateCar(car);
    }


    /**
     * 删除车辆
     * @param ids 参数
     * @return 结果
     */
    @Override
    public int delCar(Long[] ids) {
        for (Long id : ids) {
            SysIotCar car = carMapper.selectCarById(id);
            if (StringUtils.equals(Constants.YGPS_DEVICE_REPORT_STATUS,car.getReportStatus())){
                throw new ServiceException(String.format("%1$s 已上报,请先取消上报后删除", car.getCarNumber()));
            }
        }
        return carMapper.delCar(ids);
    }


    @Override
    public boolean checkCarNumberUnique(SysIotCar car) {
        Long id = StringUtils.isNull(car.getId()) ? -1L : car.getId();
        SysIotCar info = carMapper.checkCarNumberUnique(car);
        if (StringUtils.isNotNull(info) && info.getId().longValue() != id.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
